/*
* @Author: Adolphus.Yao
* @Date:   2019-08-29 15:31:04
* @Last Modified by:   Adolphus.Yao
* @Last Modified time: 2019-08-29 22:01:49
*/
package services

import (
    "fulnero.com/iris/mvc/datamodels"
    "fulnero.com/iris/mvc/repositories"
)

func NewMovieService(repo repositories.MovieRepository) MovieService {
    return &movieService{
        repo: repo,
    }
}

type movieService struct {
    repo repositories.MovieRepository
}

func (s *movieService) GetAll() []datamodels.Dao {
    return s.repo.SelectMany(func(_ datamodels.Dao) bool {
        return true
    }, -1)
}

func (s *movieService) GetByID(id int64) (datamodels.Dao, bool) {
    return s.repo.Select(func(d datamodels.Dao) bool {
        m := d.(datamodels.Movie)
        return m.ID == id
    })
}

func (s * movieService) UpdatePosterAndGenreByID(id int64, poster, genre string) (datamodels.Dao, error) {
    return s.repo.InsertOrUpdate(datamodels.Movie{
        ID:     id,
        Poster: poster,
        Genre:  genre,
    })
}

func (s *movieService) DeleteByID(id int64) bool {
    return s.repo.Delete(func(d datamodels.Dao) bool {
        m := d.(datamodels.Movie)
        return m.ID == id
    }, 1)
}
